package timing.ukulele.flow.service;

import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 构造用户，任何人均可，TODO 移入缓存
 */
@Component
public class AnyUserDetailsService implements UserDetailsService {
    private Map<String, UserDetails> userMap = new HashMap<>();
    private String[] roles = {"ROLE_ACTIVITI_USER", "ROLE_ACTIVITI_ADMIN"};

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        UserDetails user = userMap.get(username);
        if (user == null) {
            user = new User(username, "123456", Arrays.asList(roles).stream().map(SimpleGrantedAuthority::new).collect(Collectors.toList()));
            userMap.put(username, user);
        }
        return user;
    }
}
